package org.applet.game.app.rpg.resource;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class MultiImageStore extends ImageStore {

	BufferedImage[][] imageStore;
	
	BufferedImage image;
	int x;
	int y;
	
	private String sourceName;
	private int maxIndex;
	
	public MultiImageStore(String sourceName, int maxIndex)
	{
		this.maxIndex = maxIndex;
		this.sourceName = sourceName;
	}
	
	@Override
	public BufferedImage getImage() {
		return image;
	}

	@Override
	public void load() {
		imageStore = new BufferedImage[8][maxIndex+1];
		for(int i=0;i<8;i++)
		{
			for(int j=0;j<=maxIndex;j++)
			{
				imageStore[i][j] = SpriteResource.loadBodyImage(sourceName, i, j);
			}
		}
		image = (BufferedImage) imageStore[0][0];
	}

	@Override
	public void seftRender(Graphics painter, int x, int y, ImageObserver observer) {
		painter.drawImage(image, x, y, observer);
	}

	@Override
	public boolean shiftView(int x, int y) {
		image = imageStore[x][y];
		this.x = x;
		this.y = y;
		return true;
	}

	@Override
	public boolean moveViewX(int x) {
		image = imageStore[x][y];
		this.x = x;
		return true;
	}

	@Override
	public boolean moveViewY(int y) {
		image = imageStore[x][y];
		this.y = y;
		return true;
	}

	@Override
	public void saveTo(File file) {
		BufferedImage bi = new BufferedImage(image.getWidth()*(1), image.getHeight()*8, BufferedImage.TYPE_INT_ARGB);
		Graphics g = bi.getGraphics();
		for(int i=0;i<imageStore.length;i++)
		{
			BufferedImage[] rows = imageStore[i];
			for(int j=22;j<23;j++)
			{
				g.drawImage(rows[j], (j-22)*image.getWidth(), image.getHeight()*i, null);
			}
		}
		try {
			ImageIO.write(bi, "png", file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	@Override
	public boolean shiftNextX() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean shiftNextY() {
		// TODO Auto-generated method stub
		return false;
	}

}
